import type { AxiosPromise } from "axios";
import type { InputData, LayoutData } from "../types";
import { request } from "../request";

type AddTaskReqData = InputData
export interface AddTaskResData {
  "task_id": string,
  "etr": string
}

export interface TaskErrorData {
  error_code: string;
  error_msg: string;
}

export enum TaskStatus {
  Running = 'running',
  Finished = 'finished'
}

export interface TaskDetailRes {
  task_id: string;
  task_type: string;
  task_name: string;
  status: 'running' | 'finished';
  created_at: number;
  updated_at: number;
  config: InputData
  result: { objects: LayoutData[] }
}

export const reqs = {
  add: { url: '/tasks', method: 'post' },
  detail: { url: '/tasks', method: 'get' },
  list: { url: '/tasks/search-tasks', method: 'get' },
  delete: { url: '/tasks', method: 'delete' },
}

export function taskAdd(data: AddTaskReqData): AxiosPromise<AddTaskResData> {
  return request({ url: '/tasks', method: 'post', data })
}
export function taskDetail({ task_id }): AxiosPromise<TaskDetailRes> {
  return request({ url: `/tasks/${task_id}`, method: 'get' })
}
export function taskList(): AxiosPromise<AddTaskResData> {
  return request({ url: '/tasks/search-tasks', method: 'get' })
}
export function taskDelete({ task_id }): AxiosPromise<any> {
  return request({ url: `/tasks/${task_id}`, method: 'delete' })
}
